package com.sx.micowell.framework.model

import android.annotation.SuppressLint
import android.text.TextUtils
import com.example.mvvm.base.data.MutableLiveListData
import com.example.mvvm.base.data.SingleLiveEvent
import com.example.mvvm.base.vm.BaseViewModel
import com.example.mvvm.rx.RxBus
import com.example.mvvm.until.LogUtils
import com.example.mvvm.until.ToastUtils
import com.miloyu.modelnet.network.ApiResponse
import com.miloyu.modelnet.network.BaseResponse
import com.miloyu.modelnet.network.HttpClient
import com.miloyu.modelnet.network.tools.RxUtils
import com.miloyu.modelnet.response.*
import com.sx.micowell.framework.app.AppConstant
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.binding.BindingCommand
import com.sx.micowell.framework.binding.impl.IBindingAction
import com.sx.micowell.framework.tools.EventInfo
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * @Author: YY
 * @Time: 2021/8/27
 * @Describe TODO
 */
class PlanModel : BaseViewModel() {

    var dateStr: String? = null

    var planSelectData : MutableList<TrainListVO> = mutableListOf()
    var trainListEvent : MutableLiveListData<TrainListVO> = MutableLiveListData()
    var planStatusEvent: MutableLiveListData<PlanStatusVO> = MutableLiveListData()

    var addPlanSuccess: SingleLiveEvent<Void> = SingleLiveEvent()
    var deleteSuccessEvent : SingleLiveEvent<Void> = SingleLiveEvent()

    // 刷新
    val onRefreshCommand: BindingCommand<Void> = BindingCommand(IBindingAction {
        dateStr?.let { monthPlanState(it) }
    })

    // 添加计划
    val addPlanOnClickCommand = BindingCommand<Any>(IBindingAction {
        RouteManager.getInstance().intentSelectDate()
    })


    override fun registerRxBus() {
        super.registerRxBus()
        addSubscribe(
            RxBus.getDefault().toObservable(EventInfo::class.java)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { event ->
                    if (event.type == AppConstant.EVENT_ADD_TRAIN_SUCCESS) {
                        // 添加训练计划成功
                        addPlanSuccess.call()
                    }
                }
        )
    }

    /**
     * 月计划状态
     */
    @SuppressLint("CheckResult")
    fun monthPlanState(value: String) {
        addSubscribe(Observable.merge(
                HttpClient.instance.apiService.monthPlanState(value),
                HttpClient.instance.apiService.dayPlanList(value)
            ).compose(RxUtils.compose())
            .subscribeWith(object : ApiResponse<Any?>() {
                override fun success(data: Any?, msg: String?) {
                    when (data) {
                        is PlanStatusWrap -> {
                            planStatusEvent.value = data.info
                        }
                        is TrainPlanWrap -> {
                            trainListEvent.postValue(data.info)
                        }
                    }
                    ui.refreshEvent.value = true
                }

                override fun onFail(e: Throwable?, errorMsg: String?) {
                    ui.refreshEvent.value = false
                }

            })
        )

    }


    /**
     * 根据天 查询训练计划
     */
    fun dayPlanList(value: String,curriculumType :String ?=null) {
        addSubscribe(
            HttpClient.instance.apiService
            .dayPlanList(value,curriculumType)
            .compose(RxUtils.compose())
            .subscribeWith(object : ApiResponse<TrainPlanWrap>() {

                override fun success(data: TrainPlanWrap?, msg: String?) {
                    data?.info?.let {
                        trainListEvent.postValue(data.info)
                    }
                }

                override fun onFail(e: Throwable?, errorMsg: String?) {

                }

            })
        )
    }

    /**
     * 删除计划
     */
    fun deletePlan(id: String, day:String) {
        addSubscribe(
            HttpClient.instance.apiService
                .deletePlan(id, day)
                .compose(RxUtils.compose())
                .subscribeWith(object : ApiResponse<Void>() {

                    override fun success(data: Void?, msg: String?) {
                        ToastUtils.showShort("删除成功")
                        deleteSuccessEvent.call()
                    }

                    override fun onFail(e: Throwable?, errorMsg: String?) {

                    }

                })
        )
    }
}

